import axios from 'axios'

const httpCgt = async (data: any = {}, method = 'POST') => {
  const res = await axios({
    method,
    url: '/cgt-api',
    data,
  })
  return res.data.data
}

// 测试访问 IP 是否可达
async function checkIp(): Promise<boolean> {
  let reslut = false
  try {
    const controller = new AbortController()
    const timeoutId = setTimeout(() => controller.abort(), 2000)
    await fetch('/cgt-api', { signal: controller.signal })
    clearTimeout(timeoutId)
    reslut = true
  } catch (error) {
    reslut = false
  }
  return reslut
}

/**
 * 获取 Token 
 * @returns 
 */
export const getToken = async () => {

  // 测试访问 IP 是否可达
  if (await checkIp()) {
    const form = new FormData()
    form.append('data[controller]', 'account')
    form.append('data[method]', 'login')
    form.append('data[app]', 'api')
    form.append('data[account]', 'sangyang')
    form.append('data[password]', '1qaz2wsx3edc4rfv')
    const res = await httpCgt(form)
    return res.token
  } else {
    return null
  }

}

/**
 * 获取数据库列表
 * @returns 
 */
export const getDistinct = async (token: any, dbname: string) => {
  const form = new FormData()
  form.append('data[token]', token)
  form.append('data[controller]', 'project')
  form.append('data[method]', 'distinct')
  form.append('data[app]', 'api')
  form.append('data[distinct_sign]', 'project.database')
  form.append('data[sign_filter_array][0][]', 'project.database')
  form.append('data[sign_filter_array][0][]', '=')
  form.append('data[sign_filter_array][0][]', dbname)
  const res = await httpCgt(form)
  return res
}
/**
 * 
 * @param token 
 * @param dbname 
 * @returns 
 */
export const hasDatabase = async (token: any, dbname: string) => {
  const form = new FormData()
  form.append('data[token]', token)
  form.append('data[controller]', 'project')
  form.append('data[method]', 'count')
  form.append('data[app]', 'api')
  form.append('data[sign_filter_array][0][]', 'project.database')
  form.append('data[sign_filter_array][0][]', '=')
  form.append('data[sign_filter_array][0][]', dbname)
  const res = await httpCgt(form)
  return res > 0
}
/**
 * 获取任务记录 asset
 */
export const getFilterByTastAsset = async (token: string, dbName: string, userName: string) => {
  const form = new FormData()
  form.append('data[token]', token)
  form.append('data[controller]', 'task')
  form.append('data[method]', 'get_filter')
  form.append('data[app]', 'api')
  form.append('data[db]', dbName)
  form.append('data[module]', 'asset')
  form.append('data[order_sign_array][]', 'asset.entity')
  form.append('data[order_sign_array][]', 'task.pipeline_sort_id')
  // form.append('data[limit]', '100')
  // form.append('data[start_num]', '0')
  form.append('data[sign_array][]', 'asset.id')
  form.append('data[sign_array][]', 'asset.entity')
  form.append('data[sign_array][]', 'asset.cn_name')
  form.append('data[sign_array][]', 'task.pipeline_sort_id')
  form.append('data[sign_array][]', 'pipeline.entity')
  form.append('data[sign_array][]', 'task.status')
  form.append('data[sign_array][]', 'task.artist')
  form.append('data[sign_array][]', 'task.entity')
  form.append('data[sign_array][]', 'task.create_time')
  form.append('data[sign_array][]', 'task.flow_name')
  form.append('data[sign_array][]', 'task.file_status')
  form.append('data[sign_array][]', 'task.review_show_data')
  form.append('data[sign_array][]', 'task.flow_id')
  form.append('data[sign_array][]', 'task.pipeline_notice_from_wait')
  //
  form.append('data[sign_filter_array][0][]', 'task.artist')
  form.append('data[sign_filter_array][0][]', 'has')
  form.append('data[sign_filter_array][0][]', userName)
  const res = await httpCgt(form)
  return res
}

/**
 * 获取任务记录 shot
 */
export const getFilterByTastShot = async (token: string, dbName: string, userName: string) => {
  const form = new FormData()
  form.append('data[token]', token)
  form.append('data[controller]', 'task')
  form.append('data[method]', 'get_filter')
  form.append('data[app]', 'api')
  form.append('data[db]', dbName)
  form.append('data[module]', 'shot')
  form.append('data[order_sign_array][]', 'seq.entity')
  form.append('data[order_sign_array][]', 'shot.entity')
  // form.append('data[limit]', '100')
  // form.append('data[start_num]', '0')
  form.append('data[sign_array][]', 'shot.id')
  form.append('data[sign_array][]', 'shot.entity')
  form.append('data[sign_array][]', 'shot.artist')
  form.append('data[sign_array][]', 'shot.last_update_by')
  form.append('data[sign_array][]', 'seq.id')
  form.append('data[sign_array][]', 'seq.entity')
  form.append('data[sign_array][]', 'seq.artist')
  form.append('data[sign_array][]', 'task.pipeline_sort_id')
  form.append('data[sign_array][]', 'task.namex')
  form.append('data[sign_array][]', 'task.entity')
  form.append('data[sign_array][]', 'task.status')
  form.append('data[sign_array][]', 'task.artist')
  form.append('data[sign_array][]', 'task.create_time')
  form.append('data[sign_array][]', 'task.flow_name')
  form.append('data[sign_array][]', 'task.file_status')
  form.append('data[sign_array][]', 'task.review_show_data')
  form.append('data[sign_array][]', 'task.flow_id')
  form.append('data[sign_array][]', 'task.pipeline_notice_from_wait')
  //
  //
  form.append('data[sign_filter_array][0][]', 'task.artist')
  form.append('data[sign_filter_array][0][]', 'has')
  form.append('data[sign_filter_array][0][]', userName)
  const res = await httpCgt(form)
  return res
}

/**
 * 
 */
export const getCountByModule = async (token: string, dbName: string, module: module) => {
  const form = new FormData()
  form.append('data[token]', token)
  form.append('data[db]', dbName)
  form.append('data[controller]', 'task')
  form.append('data[method]', 'count')
  form.append('data[app]', 'api')
  form.append('data[module]', module)
  const res = await httpCgt(form)
  return res
}

/**
 * 
 */
export enum module {
  asset = 'asset',
  shot = 'shot'
}